/**
 * @File name: modules/list.c
 * @Synopsis:
 * @Author: xing.dai@aliyun.com
 * @Version: 0.0.1
 * @Date: 2018-08-27 16:33
 */

#include "list.h"
#include "static_pool.h"

int node_init(node_t *node)
{
    node->next = NULL;
    node->prev = NULL;
    return 0;
}

int list_init(list_t *list)
{
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
    return 0;
}

int list_add(list_t *list, node_t *node)
{

    if (list->size == 0) {
        list->head = node;
        list->tail = node;
        node->prev = NULL;
        node->next = NULL;
    } else {
        node->next = NULL;
        node->prev = list->tail;
        list->tail->next = node;
        list->tail = node;
    }
    list->size++;
    return 0;
}

int list_traversal(list_t *list, int (tra_func)(node_t *))
{
    node_t *node = NULL;

    if (list->size == 0)
        return 0;
    node = list->head;
    while (node) {
        tra_func(node);
        node = node->next;
    }
    return 0;
}

